using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using System.IO;

namespace SMS.WebApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }


        private static string logTemplate = "{Timestamp:HH:mm:ss} [{Level}] {SourceContext} {NewLine}{Message}{NewLine}{Exception}{NewLine}";

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)

            .UseServiceProviderFactory(new AutofacServiceProviderFactory())

            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>().ConfigureAppConfiguration((builderContext, config) =>
                {
                    var env = builderContext.HostingEnvironment;
                    var rootPath = "Config/";
                    config.AddJsonFile($"{rootPath}appsettings.json");
                    config.AddJsonFile($"{rootPath}appsettings.{env.EnvironmentName}.json");
                    config.AddJsonFile($"{rootPath}autofac.json");
                    config.AddJsonFile($"{rootPath}autofac.{env.EnvironmentName}.json");
                    config.AddEnvironmentVariables();
                });
            })
            .ConfigureServices((hostContext, services) =>
            {
                //appsettings.Development.json
                if (hostContext.HostingEnvironment.IsDevelopment())
                {
                    #region Serilog
                    Log.Logger = new LoggerConfiguration()
                            .ReadFrom.Configuration(hostContext.Configuration)

                            .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Information).
                             WriteTo.Async(congfig => congfig.File(
                                       Path.GetFullPath("Logs/info.txt"),
                                       rollingInterval: RollingInterval.Day,
                                       outputTemplate: logTemplate)
                             ))
                            .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Warning).
                             WriteTo.Async(congfig => congfig.File(
                                       Path.GetFullPath("Logs/warning.txt"),
                                       rollingInterval: RollingInterval.Day,
                                       outputTemplate: logTemplate)
                             ))
                            .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Error).
                             WriteTo.Async(congfig => congfig.File(
                                       Path.GetFullPath("Logs/error.txt"),
                                       rollingInterval: RollingInterval.Day,
                                       outputTemplate: logTemplate)
                             ))
                            .WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(lev => lev.Level == LogEventLevel.Fatal).
                             WriteTo.Async(congfig => congfig.File(
                                       Path.GetFullPath("Logs/fatal.txt"),
                                       rollingInterval: RollingInterval.Day,
                                       outputTemplate: logTemplate)
                             ))
                            .CreateLogger();
                    #endregion
                }
                else
                {
                    Log.Logger = new LoggerConfiguration()
                        .ReadFrom.Configuration(hostContext.Configuration)
                        .WriteTo.Sentry(o =>
                        {
                            o.Dsn = hostContext.Configuration.GetSection("Sentry:Dsn").Value;
                            o.AttachStacktrace = hostContext.Configuration.GetValue<bool>("Sentry:AttachStackTrace");
                            o.SendDefaultPii = hostContext.Configuration.GetValue<bool>("Sentry:SendDefaultPii");
                            o.DiagnosticLevel = Sentry.SentryLevel.Error;
                        })
                        .CreateLogger();
                }
            });
    }
}
